package com.example.testfocus;

import android.os.Build;
import android.support.annotation.RequiresApi;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import static android.os.Build.VERSION_CODES.N;

/**
 * Created by Reinhard Tristan Eugen Heydrich on 2016/12/12 15:20
 */
public class TimeUtils {
	
	//获取系统时间的毫秒值
	public static String getTime() {
		return String.valueOf(System.currentTimeMillis());
	}
	
	//解析时间,转换成 年-月-日格式
	public static String TimeStampDate(String timestampString){
		Long timestamp = Long.parseLong(timestampString);
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date(timestamp));
		return date;
	}
	
	//long类型毫秒时间转换成年-月-日 时:分:秒
	public static String getDateTimeStr(Long timestamp){
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp));
		return date;
	}
	
	//毫秒时间转换成月/日格式
	public static String getData(String timestampString){
		Long timestamp = Long.parseLong(timestampString);
		String date = new SimpleDateFormat("MM月dd日").format(new Date(timestamp));
		return date;
	}
	
	//毫秒时间转换成月/日 时:分:秒格式
	public static String getAllData(String timestampString){
		Long timestamp = Long.parseLong(timestampString);
		String date = new SimpleDateFormat("MM月dd日 HH:mm").format(new Date(timestamp));
		return date;
	}

	/*将字符串转为时间戳*/
	@RequiresApi(api = N)
	public static long getStringToDate(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		try{
			date = sdf.parse(time);
			} catch(Exception e) {
			e.printStackTrace();
			}
		return date.getTime();
		}

	/**
	 * 08:30
	 * @param time
	 * @return
     */
	@RequiresApi(api = N)
	public static long getRemainingTime(String time){
		String[] split = time.split(":");
		String s = TimeUtils.TimeStampDate(TimeUtils.getTime());
		s =  s + " " +  split[0] + ":" + split[1] + ":" + "00";
		long stringToDate = getStringToDate(s);
		long sum = stringToDate - System.currentTimeMillis();
		return sum;
	}

	/**
	 * 获取时间间隔
	 * @param s		1小时
	 * @return
     */
	/*public static long getTimeInterval(String s){
		if (s.equals(DONG_DONG_TIME_GAP[0])){
			return 1000*60*30;
		}else if (s.equals(DONG_DONG_TIME_GAP[1])){
			return 1000*60*60;
		}else if (s.equals(DONG_DONG_TIME_GAP[2])){
			return 1000*60*90;
		}else if (s.equals(DONG_DONG_TIME_GAP[3])){
			return 1000*60*120;
		}else if (s.equals(DONG_DONG_TIME_GAP[4])){
			return 1000*60*150;
		}else if (s.equals(DONG_DONG_TIME_GAP[5])){
			return 1000*60*180;
		}
		return -1;
	}*/

	/**
	 * 返回开始时间和结束时间之间的时间差(毫秒)
	 * @param time		格式: "08:00-20:00"
	 * @return
     */
	@RequiresApi(api = Build.VERSION_CODES.N)
	public static long getStartAndEndTimeGap(String time){
		String[] split = time.split("-");

//		split[0] = split[0].substring(0,split[0].indexOf(":")) + split[0].substring(split[0].indexOf(":") + 1,split[0].length());
//		split[1] = split[1].substring(0,split[1].indexOf(":")) + split[1].substring(split[1].indexOf(":") + 1,split[1].length());
//		int startTime = Integer.valueOf(split[0]);
//		int endTime = Integer.valueOf(split[1]);

//		return (endTime - startTime)  * 60 * 1000;
		return getRemainingTime(split[1]) - getRemainingTime(split[0]);
	}

	/**
	 * 获得指定日期的后一天
	 *
	 * @return
	 */
	public static String getSpecifiedDayAfter(Date date, int i) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + i);
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH) + 1;
		int d = c.get(Calendar.DAY_OF_MONTH);
		String dayAfter = y + "-" + m + "-" + d;
		return dayAfter;
	}


	/**
	 * 返回今天的日期
	 * @return
     */
	public static String getTodayDate(){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		return sf.format(c.getTime());
	}

	/**
	 * 判断日期是不是在指定周内,格式 "2016-5-12"
	 * @param day
	 * @param week
     * @return
     */
	public static boolean isInThisWeek(String day, String week){
//		List<String>list = new ArrayList<>();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date sunday = new Date();
		try {
			sunday = simpleDateFormat.parse(week);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < 7; i++) {
			String specifiedDayAfter = getSpecifiedDayAfter(sunday, i);
			String[] split = specifiedDayAfter.split("-");
			split[1] = split[1].length() == 1 ? "0" + split[1] : split[1];
			split[2] = split[2].length() == 1 ? "0" + split[2] : split[2];
//			list.add(split[0] + "-" + split[1] + "-" + split[2]);
			boolean a =  (day.equals(split[0] + "-" + split[1] + "-" + split[2] + " 00:00:00"));

			if (a)return true;
		}
		return false;
	}


	/**
	 * 返回上周的日期开始到结束日期
	 * true 上周开始时间  false 上周结束时间
	 * @return
     */
	public static String StringData(boolean b){
		int mYear;
		int mMonth;
		int mNowDay;
		int mNextDay;
		String m_month;
		String m_day;
		Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		Date date = new Date();
		c.setTime(date);
		int todayOfWeek = (c.get(Calendar.DAY_OF_WEEK)) - 1;
		if (!b)todayOfWeek =  todayOfWeek % 7 + 1;
		else todayOfWeek = todayOfWeek % 7 + 7;


		// 这边定义的年月日变量都要int类型 ， 不要问我为什么
		mYear = c.get(Calendar.YEAR); // 获取当前年份
		mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
		mNowDay = c.get(Calendar.DAY_OF_MONTH);// 获取当前月份的日期号码

		c.set(Calendar.DAY_OF_MONTH, mNowDay - todayOfWeek); // 延后3天
		mNextDay = c.get(Calendar.DAY_OF_MONTH);
		// 判断延后的日期小于今天的日期，月份加一
		if(mNowDay - mNextDay < 1) mMonth -= 1;
		// 判断延后的月份大于本月的月份，月份设置为一月份，年份加一
		if(mMonth>12){
			mMonth = 1;
			mYear +=1 ;
		}else if (mMonth<1){
			mMonth = 12;
			mYear -= 1;
		}
		// 测试今天的日期

		// 如果 月份为个位数则加个0在前面
		if(mMonth < 10){
			m_month = "0" + mMonth ;
		} else{
			m_month = "" + mMonth ;
		}
		// 如果 天数为个位数则加个0在前面
		if(mNextDay < 10){
			m_day = "0" + mNextDay ;
		}else{
			m_day = "" + mNextDay ;
		}
		return mYear + "-" + m_month + "-" + m_day + " 00:00:00";
	}
}